/*
 * Copyright (c) 2019-2029, <a href="https://wwww.intellijboy.cn">https://wwww.intellijboy.cn</a> (liuburu@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * <a href="https://www.apache.org/licenses/LICENSE-2.0">https://www.apache.org/licenses/LICENSE-2.0</a>
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package open.intellijboy.core.configuration.swagger;

import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import lombok.extern.slf4j.Slf4j;
import open.intellijboy.core.annotation.EnableIjFramework;
import open.intellijboy.framework.core.util.DevToolUtils;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;

import java.io.IOException;

/**
 * swagger支持
 *
 * @author intellijboy
 * @since 1.0
 */
@Slf4j
@Configuration
@ConditionalOnClass({EnableIjFramework.class})
public class SwaggerConfiguration {


    @Bean
    public OpenAPI openAPI() {
        return new OpenAPI()
                .info(new Info().title("一个拥有星辰智慧的水墨之灵的星哲小妖")
                                .description("演示 Spring MVC 架构系统设计")
                                .version("v1.0.0")
                                .license(new License().name("Apache 2.0")
                                                      .url("https://gitee.com/intellijboy/project-template")))
                .externalDocs(new ExternalDocumentation()
                        .description("Mybatis-Plus 框架")
                        .url("https://baomidou.com/")
                );
    }


    @Bean
    public GroupedOpenApi allApi() {
        return GroupedOpenApi.builder()
                             .group("全部")
                             .pathsToMatch("/**")
                             .build();
    }

    @Bean
    public SwaggerGroupLoader swaggerGroupLoader() {
        return new SwaggerGroupLoader();
    }


    /**
     * 自动打开欢迎页面
     */
    @EventListener({ApplicationReadyEvent.class})
    void applicationReadyEvent(ApplicationReadyEvent readyEvent) {
        ConfigurableApplicationContext applicationContext = readyEvent.getApplicationContext();
        ServerProperties serverProperties = applicationContext.getBean(ServerProperties.class);
        // spring-dev-tool 热重载的时候不重复打开swagger界面
        if (!DevToolUtils.isRecentBrowserOpened()) {
            String url = String.format("http://localhost:%s%s/swagger-ui.html", serverProperties.getPort(), serverProperties.getServlet()
                                                                                                                            .getContextPath());
            try {
                DevToolUtils.browseOpen(url);
            } catch (IOException e) {
                log.info("系统启动完成，无法打开目标页面：" + url);
            }
        }
    }
}
